<?php
if( !defined('IN_SEC') ){
	exit('Access Denied.');
}

final class Bug{

	var $type;
	var $msg;
	var $backtrace;

	public function __construct(){
		$this->type = 'System';
		$this->backtrace = debug_backtrace();
	}

	public function system($msg){
		$this->type = 'System';
		$this->msg = "<strong>[Error]</strong>:{$msg}";
		$this->make();
	}

	public function db($error, $sql){
		global $_G;
		if ( is_array($error) ){
			$errno = $error[1];
			$error = $error[0];
		}else{
			$errno = sprintf('%04d', 0000);
			$error = $error;
		}
		$error = str_replace(array($_G['config']['db']['name'] . '.', $_G['config']['db']['tablepre'] . '_'), array('__DB__.', '__PRE__.'), $error);

		$this->msg = "<strong>[{$errno}]</strong>:{$error}";
		if ( $sql ){
			$sql = str_replace(array('`' . $_G['config']['db']['name'] . '`.', '`' . $_G['config']['db']['tablepre'] . '_'), array('__DB__.', '__PRE__.'), $sql);
			$this->msg .= "<br /><strong>[SQL]</strong>:{$sql}";
		}
		$this->type = 'DB';
		$this->make();
	}

	public function template($name, $path){
		$path = str_replace(array(SEC_SYS_PATH, APP_PATH, 'SupEngine'), array('root', 'app', '[Derek Hide]'), $path);
		$this->msg = "<strong>[Template Not Found]:</strong>{$path}";
		$this->type = 'Template';
		$this->make();
	}

	private function backtrace(){
		$debuginfo = array();
		$backtrace = $this->backtrace;
		$skip = array(
			'class' => array('core', 'Cache'),
			'function' => array('__construct', '__template', 'call_user_func', 'execute', 'halt', 'init'),
		);
		foreach( $backtrace as $key => $one ){
			$info = array(
				'file' => str_replace(array(SEC_SYS_PATH, APP_PATH, 'supsource'), array('root', 'app', '[Derek Hide]' ), $one['file']),
				'line' => sprintf('%04d', $one['line']),
				'code' => $one['class'] . $one['type'] . $one['function'] . '();',
			);
			$debuginfo[$key] = $info;
			if ( $one['class'] ){
				if ( in_array($one['class'], $skip['class']) ){
					unset($debuginfo[$key]);
				}
			}
			if ( $one['function'] ){
				if ( in_array($one['function'], $skip['function']) ){
					unset($debuginfo[$key]);
				}
			}
		}
		return $debuginfo;
	}

	public function make(){
		$msg = $this->msg;
		$type = $this->type;
		$debuginfo = $this->backtrace();
		$debug = debuginfo();

		if( AJAX ){
			jsonp($msg, 'alert', 1);
		}

		echo <<<EOF
<DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="ROBOTS" content="NOINDEX,NOFOLLOW,NOARCHIVE" />
<title>$type Error</title>

<style>
<!--
body,html{padding:0px; margin:0px;}
ul{padding:8px;}
li {list-style:none;}
h1,h2,h3,h4,h5,h6 {font-size:100%;}
body {font:normal 14px/1.5 Tahoma,Helvetica,arial,sans-serif;}
a {color:#399;text-decoration:none;}
a:hover {text-decoration:underline;}

.debug-header{ background:#009AD9; height:70px; color:#FFF; text-align:center; line-height:60px; font-size:60px;}
.debug-msg, .debug-report{ margin:0 auto; width:1024px;background:#F3F3F3;border: 1px solid #AAA;font-size: 11pt;line-height: 160%;margin-bottom: 1em;padding: 1em; margin-top:20px;}
.debug-info{ width: 1024px; background: #F3F3F3;border: 1px solid #AAA;font-size: 11pt;line-height: 160%;margin-bottom: 1em;padding: 1em; margin:0 auto;}
.debug-report{ margin-bottom:48px; text-align: center;font: 12px verdana, arial, sans-serif;}
-->
</style>
</head>

<body>
<div class="debug-header"> $type Error !</div>

<div class="debug-msg"><span>$msg</span></div>

<div class="debug-info">
	<div class="info">
		<table cellpadding="5" cellspacing="1" width="100%" style="border:1px solid #CCC;">
			<tbody>
				<tr style="background-color: #EEE;border-bottom:1px solid #000;">
					<td>No.</td>
					<td>File</td>
					<td>Code</td>
				</tr>
EOF;
			$count = 0;
			foreach( $debuginfo as $key => $one ){
				$count++;
				foreach( $one as $k => $v ){
					$$k = $v;
				}
echo <<<EOT

				<tr style="background-color: #FFF;">
					<td>$count</td>
					<td>[$line]:$file</td>
					<td>$code</td>
				</tr>

EOT;

			}
echo <<<EOF
			</tbody>
		</table>
	</div>
</div>

<div class="debug-report">
	我们已经将此出错信息详细记录, 由此给您带来的访问不便我们深感歉意.
	<div>Processed in {$debug['time']} second(s), {$debug['queries']} queries</div>
</div>

</body>
</html>
EOF;
exit;
	}
}

?>